#include "..\server\dbcreator.h"
#include "..\server\dbstructure.h"
#include "Poco/Data/SessionFactory.h"
#include "Poco/Data/Session.h"
#include "Poco/Data/SQLite/Connector.h"
#include "Poco/StringTokenizer.h"
#include "Poco/Exception.h"

using namespace Poco::Data;
using Poco::StringTokenizer;
using Poco::File;
using Poco::Exception;
using namespace std;

void select_test_data()
{
    Poco::Data::SQLite::Connector::registerConnector();
    try
    {
        Session session(DB_TYPE, DB_NAME);
        //
        // select data and display
        //
        Statement select(session);

        db_user test;
        memset(&test, 0, sizeof(db_user));
        select << "Select * from user where id=:id",use(1),
            into(test.id),
            into(test.name),
            into(test.pass),
            into(test.hardwareid),
            into(test.email),
            into(test.key_id),
            range(0,1);

        while(!select.done())
        {
            if(select.execute() == 1)
            {
                cout << "select succesfully!" <<endl;
                std::cout<< test.id <<  " " << test.name <<  " " << test.pass <<  " " <<test.email << " " << test.hardwareid <<  " " << test.key_id << std::endl;
            }
            else
            {
                cout << "select failed!" <<endl;
            }
        }
    }
    catch (Exception& e)
    {
        cout << e.what();
    }


    try
    {
        Session s(DB_TYPE, DB_NAME);

        Statement select(s);

        db_keys key;
        
        select << "select * from keys where key=:key",use("abcdefg"),
            into(key.id),
            into(key.key),
            into(key.gentime),
            into(key.regtime),
            into(key.duration),
            into(key.state);

        while(!select.done())
        {
            if (select.execute() == 1)
            {
                cout << key.id << " | " << key.key << " | " <<key.gentime << " | " <<key.regtime << " | " <<key.duration << " | " <<key.state<<endl;
            }
            else
            {
                cout <<" select keys FAILED;"<<endl;
            }
        }
    }
    catch (Exception& e)
    {
    	cout<<e.what();
    }


}

void delete_test_data()
{
    Poco::Data::SQLite::Connector::registerConnector();
    try
    {

        Session s(DB_TYPE, DB_NAME);
        Statement del(s);

        del <<"delete from user";
        del.execute();

        del << "delete from keys";
        del.execute();
    }
    catch(Exception& e)
    {
        cout << e.what();
    }
}

void insert_test_data()
{
    Poco::Data::SQLite::Connector::registerConnector();
    try
    {
        Session session(DB_TYPE, DB_NAME);
        //
        // delete all data
        //
        Statement del(session);
        del << "delete from User";
        del.execute();

        //
        // insert test data
        //
        Statement insert(session);
        db_user test =
        {0,"admin","admin","hardwareid","40291409@163.com",1};

        insert << "INSERT INTO user (name,pass,email,hardwareid,key) VALUES (?,?,?,?,?)",
            use(test.name),
            use(test.pass),
            use(test.hardwareid),
            use(test.email),
            use(test.key_id);

        if(insert.execute() == 1)
        {
            cout << "insert user data successfully!"<<endl;
        }

        Statement insert1(session);
        db_keys k = {0,"abcdefg",1,2,3,0};
        insert1 << "INSERT INTO keys (key, gentime, regtime, duration, status) VALUES (?,?,?,?,?)",
            use(k.key),use(k.gentime),use(k.regtime),use(k.duration),use(k.state);

        if (insert1.execute() == 1)
        {
            cout << "insert keys data successfully!"<<endl;
        }
    }
    catch (Exception& e)
    {
        cout << "exception occurred while operating database!"<<endl;
    	cout << e.what()<<endl;
    }
}

int main(int argc, char* argv[])
{
    File f(DB_NAME);
    if (f.exists())
    {
        f.remove();
    }


    DBCreator db(DB_NAME,DB_SCHEMA);

    if(db.CheckDatabase())
    {
        printf("check successfully!");
        insert_test_data();
        select_test_data();
        //delete_test_data();
        return 1;
    }

    printf("check failed!");
    return 0;
}